<?php

    class Rezultati_m extends Model {

        function Rezultati_m () {
            parent::Model();
        }

        public function citaj_rezultate($id){ /* return:object (title, description, keywords) */
            //Let's store the poll data for number of answers in our array). We also store
            //the name of answers.
            /*
            --------------------------------------
            arrData[0][1] ->    answers_text
            arrData[0][2] ->    no_votes
            --------------------------------------
            --------------------------------------
            arrData[1][1] ->    answers_text
            arrData[1][2] ->    no_votes
            --------------------------------------
            --------------------------------------
            arrData[2][1] ->    answers_text
            arrData[2][2] ->    no_votes
            --------------------------------------
            --------------------------------------
            arrData[3][1] ->    answers_text
            arrData[3][2] ->    no_votes
            --------------------------------------
            --------------------------------------
            arrData[0][0] ->    total_votes
            --------------------------------------
            */
            $arrData;
            $rezultati = $this->db->get_where('votes', array('poll_id'=> $id))->result();
            $odgovor = $this->db->order_by('position', 'asc')->get_where('answer', array('poll_id'=> $id))->result();
            $total_votes = 0;
            $no_answers = 0; 
            foreach($rezultati as $res){ $total_votes++; }
            $arrData[0][0] = $total_votes;
            if($total_votes == 0) return $arrData;
            foreach($odgovor as $res){ $no_answers++; }
            
            $i=0;
            foreach($odgovor as $row){
                $arrData[$i][1] = $row->text;
                $cnt=0;
                foreach($rezultati as $res){
                    if($res->answer_id==$row->id)$cnt++;    
                }                
                $arrData[$i][2] = $cnt;              
                $arrData[$i][3] = (100*$cnt)/$total_votes;         /*COUNT PERCENTAGE VALUE --> FORMULA X:100=CUR:TOTAL*/ 
            
                $i++;
            }        

            return $arrData;
        } 
        public function citaj_naziv($id){
            $rezultati = $this->db->get_where('poll', array('id'=> $id))->result();
            foreach($rezultati as $res){
                return $res->question;   
            }
        }
        public function citaj_anketu($id){
            return $this->db->get_where('poll', array('id'=> $id))->result(); 
        }
    }
?>